<?php


use app\modules\Admin\models\dianpuapplyModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class dianpuModel extends Model
{
    use SoftDeletes;

    protected $table = 'dianpu';
    protected $primaryKey = 'id';
    protected $appends = ['status_name'];
    protected $hidden = [];

    // 分销权限码 使用位运算处理
    const JICHAFENHONG_PERMISSION = 1;  // 代理级差分红权限码
    const PINGJIFENHONG_PERMISSION = 2;  // 代理平级分红权限码
    const TUIKEFENHONG_PERMISSION = 4;  // 推客权限码

    protected static $commissionPermissionArray = [
        'jichafenhong'=>[
            'name'=>'',
            'permission'=> self::JICHAFENHONG_PERMISSION,
        ],
        'pingjifenhong'=>[
            'name'=>'',
            'permission'=> self::PINGJIFENHONG_PERMISSION,
        ],
        'tuikefenhong'=>[
            'name'=>'',
            'permission'=> self::TUIKEFENHONG_PERMISSION,
        ],
    ];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function getCommissionPermission()
    {
        $list = self::$commissionPermissionArray?:[];
        if(is_array($list) && !empty($list)){
            foreach ($list as $key => &$item){
                if($key === 'jichafenhong'){
                    $name = configModel::query()->where('type', 19)->where('key', 'jichafenhong_text')->value('value');
                    $item['name'] = $name?:'级差分红';
                }elseif ($key === 'pingjifenhong'){
                    $name = configModel::query()->where('type', 19)->where('key', 'pingjifenhong_text')->value('value');
                    $item['name'] = $name?:'平级分红';
                }elseif ($key === 'tuikefenhong'){
                    $name = configModel::query()->where('type', 19)->where('key', 'tuikefenhong_text')->value('value');
                    $item['name'] = $name?:'推客分红';
                }
            }
        }
        return $list;
    }

    public function getStatusNameAttribute($value)
    {
        $status_name = '禁用';

        if ($this->attributes['status'] > 0) {
            $status_name = '正常';
        }

        return $status_name;
    }

    public function dianpuapply()
    {
        return $this->hasOne(dianpuapplyModel::class, 'apply_id', 'id');
    }

    public function member()
    {
        $member_table = (new \memberModel())->getTable();
        $merchant_table = (new \app\modules\Admin\models\merchantModel())->getTable();
        return $this->hasManyThrough(\memberModel::class,
                                     \app\modules\Admin\models\merchantModel::class,
                                     'dianpu_id',
                                     'id',
                                     'id',
                                     'member_id')->select(["{$member_table}.id",
                                                           "{$member_table}.phone",
                                                           "{$member_table}.nickname",
                                                           "{$member_table}.realname",
                                                           "{$member_table}.avatar",
                                                           "{$merchant_table}.is_root"])->where("{$merchant_table}.is_root", '=', 1);
    }

    public function banner()
    {
        return $this->hasMany(\app\modules\Merchant\models\bannerModel::class, 'dianpu_id', 'id');
    }
}